/*** MATH Score Schedules FOR SPRING 2012-2019 ***/
/*
This script organizes the score schedules for STAAR math tests
administered in spring 2012-2019. Modified score schedules are
included, and extended back to 2012 by replicating the 2014
modified schedule.
*/

set more 1
clear all

do "paths.do"

capture log close
log using "$LOGFILES\tracking_0.log",replace


** standard scale
import excel "E:\projects\107-Tracking\NewFilesReleased\Math Score Schedules\STAAR math test scores.xlsx", sheet("STAAR") clear
sxpose, clear
drop if _n == 1

rename _var1 year
rename _var2 grade
rename _var3 version
rename _var4 date
rename _var5 exam_type

save "$WORKING\temp\tracking_0_1", replace


** modified scale
import excel "E:\projects\107-Tracking\NewFilesReleased\Math Score Schedules\STAAR math test scores.xlsx", sheet("STAARMod") clear
sxpose, clear
drop if _n == 1

rename _var1 year
rename _var2 grade
rename _var3 version
rename _var4 date
rename _var5 exam_type

save "$WORKING\temp\tracking_0_2", replace


** alternate scale
import excel "E:\projects\107-Tracking\NewFilesReleased\Math Score Schedules\STAAR math test scores.xlsx", sheet("STAARAlt2") clear
sxpose, clear
drop if _n == 1

rename _var1 year
rename _var2 grade
rename _var3 exam_type
gen version = "alt"
gen date = ""

save "$WORKING\temp\tracking_0_3", replace


** append scales together and reshape
use "$WORKING\temp\tracking_0_1", clear
append using "$WORKING\temp\tracking_0_2"
append using "$WORKING\temp\tracking_0_3"

reshape long _var, i(year grade version date exam_type) j(raw_score)

rename _var scale_score
replace raw_score = raw_score - 4
replace raw_score = raw_score - 2 if (version != "alt")

drop if scale_score == ""

save "$WORKING\temp\tracking_0_4", replace


/**********************************

Cleaning scales

***********************************/

** test versions
gen test_ver = ""
replace test_ver = "A" if inlist(version, "A", "AP")
replace test_ver = "T" if inlist(version, "alt")
replace test_ver = "S" if inlist(version, "BR", "ES", "ESSP", "REG", "SP", "SPAN")
replace test_ver = "L" if inlist(version, "L", "LP")
replace test_ver = "M" if inlist(version, "MOD", "MODBR")

gen month = .
replace month = 0 if (date == "SPRING")
replace month = 3 if (date == "MARCH")
replace month = 4 if (date == "APRIL")
replace month = 5 if (date == "MAY")
replace month = 6 if (date == "JUNE")

keep year month grade exam_type test_ver scale_score raw_score
order year month grade exam_type test_ver scale_score raw_score

save "$WORKING\temp\tracking_0_5", replace


** Drop duplicates
use "$WORKING\temp\tracking_0_5", clear

sort year grade exam_type test_ver month raw_score scale_score
duplicates tag year grade test_ver scale_score, generate(tmp1)
* note: raw_score and month can differ across duplicates
duplicates tag year grade test_ver scale_score raw_score, generate(tmp2)
duplicates tag year grade test_ver scale_score month, generate(tmp3)
tab tmp1 tmp2, m
tab tmp1 tmp3, m
* note: as example below shows, by design exam type differs across duplicates
list if (year == "2012") & (grade == "3") & (test_ver == "S") & (scale_score == "770")
egen tmp4 = tag(year grade test_ver scale_score)
tab tmp1 tmp4, m
tab tmp1 year if (tmp4 == 1)
drop if (tmp4 != 1)
* note: among duplicates in (year grade test_ver scale_score),
** month exam_type raw_score are chosen arbitrarily but deterministically
drop tmp*

save "$WORKING\temp\tracking_0_6", replace


** Replicate modified schedules 2014 in 2013 and 2012

for var year grade scale_score raw_score: destring X, replace
summ if (year == 2014) & (test_ver == "M")
expand 3 if (year == 2014) & (test_ver == "M")
bys year grade test_ver scale_score: replace year = year + 1 - _n if (year == 2014) & (test_ver == "M")
tab year if test_ver == "M"

order year grade test_ver scale_score
sort year grade test_ver scale_score

save "$WORKING\tracking_0.dta", replace
drop month exam_type raw_score
export delimited "$WORKING\tracking_0-schedules.csv", replace



** Ranges

use "$WORKING\tracking_0.dta", clear
tab test_ver, m
keep if inlist(test_ver, "A", "S", "L")
collapse (min) scale_score_min=scale_score (max) scale_score_max=scale_score, by(year grade)
export delimited "$WORKING\tracking_0-regRanges.csv", replace


log close
